import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hygf/page/home/application/state.dart';

import '../../../config/StyleConfig.dart';
import '../../../res/HDColors.dart';
import '../../../util/ScreenDevice.dart';
import 'logic.dart';

/// 应用
class ApplicationPage extends StatelessWidget {
  final logic = Get.put(ApplicationLogic());
  final state = Get.find<ApplicationLogic>().state;

  late double barHeight;

  @override
  Widget build(BuildContext context) {
    barHeight = ScreenDevice.getDeviceTopHeight(context);

    return Container(
        color: HDColors.grayBg,
        child: Stack(
          children: [
            Container(
                margin: const EdgeInsets.only(top: kToolbarHeight),
                child: ListView(
                  children: [
                    GetBuilder<ApplicationLogic>(
                      builder: (logic) {
                        return Visibility(
                            visible: state.showClassifiedView,
                            child: SingleChildScrollView(
                              scrollDirection: Axis.horizontal,
                              child: Container(
                                height: 45,
                                color: HDColors.classifiedBg,
                                child: Row(
                                  children: [
                                    Container(
                                        margin: const EdgeInsets.fromLTRB(
                                            15, 10, 15, 8),
                                        padding: const EdgeInsets.only(
                                            left: 8, right: 8),
                                        decoration: BoxDecoration(
                                            color: HDColors.red,
                                            borderRadius:
                                                BorderRadius.circular(5)),
                                        child: const Center(
                                          child: Text("授权企业",
                                              style: TextStyle(
                                                  color: HDColors.white,
                                                  fontSize: StyleConfig
                                                      .fontContentSize16)),
                                        )),
                                    Text(state.classifidedText,
                                        style: const TextStyle(
                                            color: HDColors.red, fontSize: 15)),
                                    InkWell(
                                        onTap: () {
                                          state.showClassifiedView =
                                              !state.showClassifiedView;
                                          logic.update();
                                        },
                                        child: Container(
                                            margin: const EdgeInsets.fromLTRB(
                                                0, 0, 10, 0),
                                            child: const Icon(Icons.cancel,
                                                color: HDColors.red,
                                                size: 20))),
                                  ],
                                ),
                              ),
                            ));
                      },
                    ),
                    // Container(
                    //   height: 210,
                    //   child:  Image.asset(
                    //     "assets/images/home_top_bg.jpg",
                    //     fit: BoxFit.scaleDown,
                    //   )
                    // ),
                    Visibility(
                        visible: state.business,
                        child: Column(
                          children: [
                            buildTextTitle("商务档案"),
                            buildList(1),
                          ],
                        )),
                    Visibility(
                        visible: state.takan,
                        child: Column(
                          children: [
                            buildTextTitle("踏勘环节"),
                            buildList(2),
                          ],
                        )),
                    Visibility(
                        visible: state.startApproval,
                        child: Column(
                          children: [
                            buildTextTitle("开工报审"),
                            buildList(3),
                          ],
                        )),
                    Visibility(
                        visible: state.construction,
                        child: Column(
                          children: [
                            buildTextTitle("施工环节"),
                            buildList(4),
                          ],
                        )),
                    Visibility(
                        visible: state.gridNet,
                        child: Column(
                          children: [
                            buildTextTitle("并网环节"),
                            buildList(5),
                          ],
                        )),
                    Visibility(
                        visible: state.operationToll,
                        child: Column(
                          children: [
                            buildTextTitle("运维环节"),
                            buildList(6),
                          ],
                        )),
                    Container(height: 15)
                  ],
                )),
            Container(color: HDColors.green, child: buildTitleBar())
          ],
        ));
  }

  /// 标题
  buildTitleBar() {
    return Container(
        height: barHeight + kToolbarHeight,
        width: double.infinity,
        padding: EdgeInsets.only(top: barHeight),
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [Color(0x8007C160), Color(0x0007C160)],
          ),
        ),
        child: Stack(
          alignment: const FractionalOffset(0, 0.5),
          children: const [
            Align(
              alignment: FractionalOffset(0.5, 0.5),
              child: Text("华裕光伏",
                  style: TextStyle(
                      fontSize: 18,
                      color: HDColors.white,
                      fontWeight: FontWeight.w600)),
            ),
          ],
        ));
  }

  buildTextTitle(String text) {
    return Container(
        margin: const EdgeInsets.fromLTRB(15, 15, 15, 15),
        child: Row(
          children: [
            Container(
              width: 4,
              height: 12,
              color: HDColors.green,
            ),
            Padding(
                padding: const EdgeInsets.only(left: 12),
                child: Text(text,
                    style: const TextStyle(
                        fontSize: StyleConfig.fontContentSize16,
                        color: HDColors.black,
                        fontWeight: FontWeight.w600)))
          ],
        ));
  }

  /// type：1:商务，勘探，巡检
  buildList(int type) {
    List<PermitItem> list = [];
    if (type == 1) {
      list = state.businessList;
    } else if (type == 2) {
      list = state.explorationList;
    } else if (type == 3) {
      list = state.startApprovalList;
    } else if (type == 4) {
      list = state.constructionList;
    } else if (type == 5) {
      list = state.bingWangList;
    }else if (type == 6) {
      list = state.operationTollList;
    }

    return Container(
        margin: const EdgeInsets.only(left: 15, right: 15),
        child: GridView.builder(
          itemCount: list.length,
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              mainAxisSpacing: 1,
              crossAxisSpacing: 1,
              childAspectRatio: 1),
          itemBuilder: (BuildContext context, int index) {
            return buildItem(type, index);
          },
        ));
  }

  /// type：1:商务，勘探
  buildItem(int type, int index) {
    dynamic item;
    if (type == 1) {
      item = state.businessList[index];
    } else if (type == 2) {
      item = state.explorationList[index];
    } else if (type == 3) {
      item = state.startApprovalList[index];
    } else if (type == 4) {
      item = state.constructionList[index];
    } else if (type == 5) {
      item = state.bingWangList[index];
    } else if (type == 6) {
      item = state.operationTollList[index];
    }

    return InkWell(
      onTap: () => logic.clickPermitItem(type, item),
      child: Container(
        color: HDColors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(item.icon, width: 50, height: 50),
            Text(
              item.bottomText,
              style: const TextStyle(
                color: HDColors.black,
                fontSize: StyleConfig.fontContentSize16,
              ),
            )
          ],
        ),
      ),
    );
  }
}
